﻿using System.Collections.Specialized;
using System.IO;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Services.Interfaces.Netflix;
using Rodunu.Movies.Services.Models.Netflix;
using Rodunu.Movies.Services.NetFlix.DataProvider;

namespace Rodunu.Movies.Services.NetFlix.Logic
{
    public class ManageQueue : NetflixServiceBase, IManageQueue
    {
        #region Constructors

        public ManageQueue(string tokenKey)
            : base(tokenKey)
        {
            
        }

        public ManageQueue()
        {

        }

        public ManageQueue(UserInfo userId)
            : base(userId)
        {

        }

        #endregion

        #region IManageQueue Members

        public Queue GetDiscQueueAvailable(int startIndex)
        {
            var parameters = new NameValueCollection();
            parameters.Add("start_index", startIndex.ToString());
            parameters.Add("max_results", "25");
            return (Queue)
                _serializationHelper.FromXml(typeof(Queue),
                                            new StreamReader(
                                                _netflixService.GetUserResource("http://api.netflix.com/users/{userid}/queues/disc/available", parameters).GetResponseStream()).ReadToEnd());
        }

        public Queue GetUserQueue(int startIndex, int maxResults)
        {
            var parameters = new NameValueCollection();
            parameters.Add("start_index", startIndex.ToString());
            if (maxResults == 0)
                parameters.Add("max_results", "25");
            else
                parameters.Add("max_results", maxResults.ToString());
            return (Queue)
                _serializationHelper.FromXml(typeof(Queue),
                                            new StreamReader(
                                                _netflixService.GetUserResource("http://api.netflix.com/users/{userid}/queues/disc", parameters).GetResponseStream()).ReadToEnd());
        }

        public QueueUpdateStatus UpdateDiscQueue(QueueItem queueItem, Queue queue)
        {
            var parameters = new NameValueCollection();
            parameters.Add("title_ref", queueItem.TitleRef);
            parameters.Add("format", "DVD");
            parameters.Add("position", queueItem.Position.ToString());
            parameters.Add("etag", queue.ETag.ToString());
            return (QueueUpdateStatus)
                _serializationHelper.FromXml(typeof(QueueUpdateStatus),
                                            new StreamReader(_netflixService.Post(string.Format("http://api.netflix.com/users/{0}/queues/disc", _netflixService.UserId), parameters).GetResponseStream()).ReadToEnd());
        }

        public Queue GetInstantWatchQueue(int startIndex)
        {
            var parameters = new NameValueCollection();
            parameters.Add("start_index", startIndex.ToString());
            parameters.Add("max_results", "25");
            return (Queue)
                _serializationHelper.FromXml(typeof(Queue), new StreamReader(_netflixService.GetUserResource("http://api.netflix.com/users/{userid}/queues/instant", parameters).GetResponseStream()).ReadToEnd());
        }

        public QueueUpdateStatus UpdateInstantWatchQueue(QueueItem queueItem, Queue queue)
        {
            var parameters = new NameValueCollection();
            parameters.Add("title_ref", queueItem.TitleRef);
            parameters.Add("position", queueItem.Position.ToString());
            parameters.Add("etag", queue.ETag.ToString());
            return (QueueUpdateStatus)
                _serializationHelper.FromXml(typeof(QueueUpdateStatus), new StreamReader(_netflixService.Post(string.Format("http://api.netflix.com/users/{0}/queues/instant",
                    _netflixService.UserId), parameters).GetResponseStream()).ReadToEnd());
        }

        public Status DeleteQueueEntry(QueueItem queueItem)
        {
            var parameters = new NameValueCollection();
            return (Status)
                _serializationHelper.FromXml(typeof(Status),
                new StreamReader(_netflixService.Delete(queueItem.Id, parameters).GetResponseStream()).ReadToEnd());
        }

        #endregion
    }
}
